package com.cqupt.controller;
import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.BasicalItemService;
import com.cqupt.service.MonitorService;
import com.cqupt.service.impl.MonitorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RequestMapping("basical")
@Controller
@ResponseBody
public class BasicalItemController {
    @Autowired
    private BasicalItemService basicalItemService;
    @Autowired
    private MonitorService monitorService;

    /**
     *
     * @param hostId 给定某个主机
     * @return 返回该主机的某个监控项值
     */
    @RequestMapping("/agentItem")
    public ResponseMessage getHostBasicItems(@RequestParam(value = "hostId") String hostId,@RequestParam(value = "itemName") String itemName,@RequestParam(value = "type") String type){
        List<Map<String, Object>> basicItem = basicalItemService.getUtilizationByName(hostId,itemName,type);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", basicItem);
        }};
        ResponseMessage responseMessage=new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 获得监控项实时的值
     * @param hostId
     * @return
     */
    @RequestMapping("/agentRealData")
    public ResponseMessage getHostBasicItems(@RequestParam(value = "hostId") String hostId){
        List<Map<String, Object>> basicItem = basicalItemService.getRealDataByName(hostId);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", basicItem);
        }};
        ResponseMessage responseMessage=new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 根据主机id获得所有该主机的监控项
     * @param hostId
     * @param curPage
     * @param pageSize
     * @param checkList
     * @return
     */
    @RequestMapping("/allItems")
    public ResponseMessage getHostAllItems(@RequestParam(value = "hostId")String hostId,@RequestParam(value = "curPage")String curPage,@RequestParam(value = "pageSize")String pageSize,
    @RequestParam (value = "currentitemids",required = false)String[] checkList)
    {
        List<Map<String, Object>> allItems = basicalItemService.getAllItems(hostId,curPage,pageSize,checkList);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", allItems);
        }};
        ResponseMessage responseMessage=new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 根据主机的ID获取该主机的IP地址
     * @param hostId
     * @return
     */
    @RequestMapping("/getIP")
    public ResponseMessage getIPByHostId(@RequestParam(value = "hostId")String hostId) {
        ArrayList<String> list = new ArrayList<>();
        list.add(hostId);

        List<Map<String, Object>> hostInterfaceListByHostIds = monitorService.getHostInterfaceListByHostIds(list);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", hostInterfaceListByHostIds);
        }};
        ResponseMessage responseMessage=new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 根据itemID获取键值，并返回20条最新数据
     * @param itemID
     * @param hostID
     * @return
     */
    @RequestMapping("/getDataByItemID")
    public ResponseMessage getDataByItemID(@RequestParam(value = "itemID")String itemID,@RequestParam(value = "hostID")String hostID) {
        List<Map<String, Object>> dataByItemID = basicalItemService.getDataByItemID(hostID,itemID);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", dataByItemID);
        }};
        ResponseMessage responseMessage=new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 根据hostId查询磁盘容量 （服务器设备） 暂时弃用
     * @param hostId
     * @return
     */
    //public ResponseMessage getHostDisctSize(@RequestParam(value = "hostId") String hostId){
    //    List<Map<String, Object>> basicItem = basicalItemService.getRealDataByName(hostId);
    //    Map<String, Object> resultMap = new HashMap<String, Object>(){{
    //        put("result", basicItem);
    //    }};
    //    ResponseMessage responseMessage=new ResponseMessage();
    //    responseMessage.setStatus(true);
    //    responseMessage.setContent(resultMap);
    //    return responseMessage;
    //}
}
